package com.itzixi.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itzixi.common.utils.LeeJSONResult;
import com.itzixi.pojo.SysUser;
import com.itzixi.service.UserService;
import com.itzixi.service.email.EmailService;
import com.itzixi.shiro.util.PasswordHelper;

@Controller
@RequestMapping("/pwd")
public class RegistController {

	@Autowired
	private UserService userService;

	@Autowired
	private EmailService emailService;

	/**
	 * 注册页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "regist", method = RequestMethod.GET)
	public String regist() {
		return "regist";
	}

	/**
	 * 提交注册
	 * 
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "regist", method = RequestMethod.POST)
	@ResponseBody
	public LeeJSONResult doRegist(SysUser sysUser) {
		PasswordHelper.encryptPassword(sysUser);
		userService.saveRegistEmail(sysUser);
		return LeeJSONResult.ok();
	}

	/**
	 * 重置密码
	 * 
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "resetPwd", method = RequestMethod.POST)
	@ResponseBody
	public LeeJSONResult resetPwd(SysUser sysUser) {
		userService.resetPwd(sysUser);
		return LeeJSONResult.ok();
	}

	/**
	 * 提交注册,重置密码后发邮件
	 * 
	 * @param type
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "sendEmail", method = RequestMethod.POST)
	@ResponseBody
	public LeeJSONResult sendEmail(String type, SysUser sysUser) {
		emailService.sendEmail(type, sysUser);
		return LeeJSONResult.ok();
	}

	/**
	 * 点击激活连接返回页面
	 * 
	 * @param validateCode
	 * @param email
	 * @return
	 */
	@RequestMapping(value = "active", method = RequestMethod.GET)
	public String active(String validateCode, String email) {
		try {
			userService.updateActiveEmail(validateCode, email);
			return "login";
		} catch (Exception e) {
			e.printStackTrace();
			return "regist";
		}

	}

	/**
	 * 重置密码页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "resetPassword", method = RequestMethod.GET)
	public String resetPassword(String email,String validateCode,ModelMap map) {
		map.put("email", email);
		map.put("validateCode", validateCode);
		return "resetPwd";
	}

	/**
	 * 重置密码页面
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "resetPassword", method = RequestMethod.POST)
	@ResponseBody
	public LeeJSONResult doResetPassword(SysUser sysUser){
		SysUser user = userService.findByUsername(sysUser.getUsername());
		if(sysUser.getActiveCode().equals(user.getActiveCode())){
			try {
				userService.updateRestPwd(sysUser.getActiveCode(), sysUser.getUsername());
				user.setPassword(sysUser.getPassword());
				PasswordHelper.encryptPassword(user);
				return LeeJSONResult.ok();
			} catch (Exception e) {
				return LeeJSONResult.errorMsg("重置码失效,请重试");
			}
		}else{
			return LeeJSONResult.errorMsg("重置密码失败,请重试");
		}
		
	}
}
